package com.example.skycode5.servlet;

import com.example.skycode5.Bean.NewsBean;
import com.example.skycode5.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/newsAdd.jsp")
public class NoticeAddSerlvet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String newsTitle = req.getParameter("NewsTitle");
        String newsContent = req.getParameter("NewsContent");
        NewsBean newsBean = new NewsBean();//实例化新闻对象
        newsBean.setNewstitle(newsTitle);//设置新闻标题
        newsBean.setNewscontent(newsContent);//设置新闻内容
        NewsDao newsDao = new NewsDao();//实例化新闻DAO
        newsDao.addNews(newsBean);//调用添加方法 传递新闻对象
        //  在SERLET有页面定向功能 可以将页面跳转到指定位置
        resp.sendRedirect("news.jsp");//参数是相对网址                               3
    }
}
